
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        :permission="permissionList"
        v-model="form.swStTvmCheckDetail"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
      >
        <template slot="menuLeft">
          <el-button type="primary" size="small"     @click="delBatch">删 除</el-button>
          <div style="padding-left: 30px;display:inline-block;">
            <span style="color: red;float:left;font-size: 12px">提示：必须在钱箱卸载后再录入实际卸载金额，录入后该条数据不可更改！</span>
          </div>

          <el-button type="primary" size="small" v-if="false" @click="handleExport">导 出</el-button>

        </template>
        <!-- <template slot="menuForm">
          <el-button type="primary" size="small" @click="submit">清点人确认</el-button>
        </template> -->
        <template slot="menu" slot-scope="scope">
          <el-button
            icon="el-icon-edit"
            :size="scope.size"
            :type="scope.type"
            v-if="scope.row.isSubmit=='0'"
            v-permission="permissionList.editBtn"
            @click.stop="handleEdit(scope.row)"
          >编 辑</el-button>
          <!-- <el-button
            icon="el-icon-edit"
            :size="scope.size"
            :type="scope.type"
            v-if="scope.row.isSubmit=='0'"
            v-permission="permissionList.editBtn"
            @click.stop="rowEdit(scope.row, scope.index)"
            @click="Iedit"
          >编 辑</el-button> -->
          <!-- <el-button
            icon="el-icon-check"
            :size="scope.size"
            :type="scope.type"
            @click.stop="myView(scope.row)"
          >自定义预览</el-button> -->
        </template>
      </avue-crud>
    </basic-container>
    <div>
      <el-dialog
        :visible="editDialogFormVisible"
        width=60%
        title="编辑"
      >
        <el-form v-if="editDialogFormVisible">
          <avue-form v-model="formEdit" :option="tableOption"></avue-form>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button type="primary" size="small" @click="editSave">保 存</el-button>
            <el-button size="small" @click="editDialogFormVisible = false"
              >取 消</el-button
            >
            <el-button type="primary" size="small" @click="submit">清点人确认</el-button>
        </div>
      </el-dialog>
      <el-dialog
        width=30%
        title="清点人密码"
        :visible="dialogFormVisible"
        :showClose='false'
        append-to-body
        center
        :destroy-on-close="true"
      >
        <el-form :model="form1" ref="ruleForm">
          <el-form-item label="清点人密码" prop="password">
            <el-input v-model="form1.password" type="password" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button size="small" @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" size="small" @click="confirmUser">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  delsObj,
  exportExcel,
} from "@/api/subwaystm/swstinputcashboxuninstalled";
import { checkPwd } from "@/api/admin/user";
import { tableOption } from "@/const/crud/subwaystm/swstinputcashboxuninstalled";
import { mapGetters } from "vuex";
import baseCrud from '@/mixins/base-crud';
import { dateFormat } from "@/util/date";

export default {
  name: "swstinputcashboxuninstalled",
  mixins: [baseCrud('subwaystm/swstinputcashboxuninstalled')],
  data() {
    return {
      form1: {
        password:''
      },
      formEdit: {},
      editDialogFormVisible: false,
      tableOption: tableOption,
      viewDialogVisible: false,
      viewchangeDialogFlag: false,
      editDialogVisible: false,
      editchangeDialogFlag: false,
      dialogFormVisible: false,
      saveFlag: false,
      jobNo:'',
      rules: {
      password:[
        {required:true,message:'请输入清点人密码',trigger:'blur'}
      ]
      }
    };

  },
  watch:{
    'form.swStTvmCheckDetail.uninstallAmount':function(){
      this.form.swStTvmCheckDetail.differenceAmount=Number(this.form.swStTvmCheckDetail.uninstallAmount)-Number(this.form.swStTvmCheckDetail.shouldBeUnloadedAmount)
    },
    ThisMain() {
      this.form.swStTvmCheckDetail = this.ThisMain;
    },
  },
  props: {
    ThisMain: Object
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(
          this.permissions.generator_swstinputcashboxuninstalled_add,
          false
        ),
        delBtn: this.vaildData(
          this.permissions.generator_swstinputcashboxuninstalled_del,
          false
        ),
        editBtn: this.vaildData(
          this.permissions.swstinputcashboxuninstalled_edit,
          false
        ),
        form: {
          swStTvmCheckDetail: {}
        }
      };

    },
  },
  methods: {
      // Iedit: function(){
      //   if(this.validatenull(this.form.swStTvmCheckDetail.uninstallDate)){
      //     let now = new Date();
      //     if(now.getHours() < 6){
      //       now.setDate(now.getDate() - 1)
      //     }
      //     this.form.swStTvmCheckDetail.uninstallDate=dateFormat(now).substring(0, 10);
      //   }
      // },
      handleEdit(row){
        this.saveFlag =false;
        this.formEdit = row;
        this.editDialogFormVisible = true;
        if(this.validatenull(this.formEdit.uninstallDate)){
          let now = new Date();
          if(now.getHours() < 6){
            now.setDate(now.getDate() - 1)
          }
          this.formEdit.uninstallDate=dateFormat(now).substring(0, 10);
        }
      },
      editSave(){
        if(!this.saveFlag){
          this.$message.error("请先点击清点人确认按钮进行确认！");
          return;
        }
        putObj(this.formEdit)
          .then((data) => {
            this.$message.success("编辑成功");
            this.getList(this.page);
            this.editDialogFormVisible = false;
            done();
          })
          .catch(() => {});
      },
      submit: function(){
        this.form1={};
        this.dialogFormVisible=true
      },
      myView: function (row) {
      getCascadeObj(row.id).then((response) => {
        let res = response.data.data;
        console.log(res.electronicReceipts);
        this.detailData = res.electronicReceipts;
        this.accountTime = res.receipt.accountTime;
      });
      this.viewDialogVisible = true;
    },
    beforeRowUpdate: function (row, index, callback, loading) {
      this.jobNo = row.inventoryPersonJobNo;
      if(!this.saveFlag){
        this.$message.error("请先点击清点人确认按钮进行确认！");
        loading();
      }else{
        callback();
        this.saveFlag =false;
      }
    },
    viewchangeDialog: function () {
      if (this.viewchangeDialogFlag) {
        this.viewchangeDialogFlag = false;
      } else {
        this.viewchangeDialogFlag = true;
      }
    },
    myEdit: function (row) {
      getCascadeObj(row.id).then((response) => {
        let res = response.data.data;
        console.log(res.electronicReceipts);
        this.detailData = res.electronicReceipts;
        this.accountTime = res.receipt.accountTime;
      });
      this.viewDialogVisible = true;
    },
    editchangeDialog: function () {
      if (this.editchangeDialogFlag) {
        this.editchangeDialogFlag = false;
      } else {
        this.editchangeDialogFlag = true;
      }
    },
    delBatch: function () {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请勾选数据");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.uninstallDate != "" && table.uninstallDate!=  null) {
          this.$message.error("请检查数据，只能删除未卸载的");
          return false;
        }
        ids.push(table.id);
      }
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        delsObj(ids)
          .then((data) => {
            this.$message.success("删除成功");
            this.getList(this.page);
            done();
          })
          .catch(() => {
            loading();
          });
        });
    },
    confirmUser: function () {
      if(this.formEdit.inventoryPersonJobNo ==''){
              this.$message.error("请选择清点人！");
      }else if(this.form1.password.length==0){
              this.$message.error("请输入清点人密码!");
      }else{
        checkPwd(this.formEdit.inventoryPersonJobNo, this.form1.password).then((response) => {
          let data = response.data.data;
          if (data) {
            this.$message.success("输入正确");
            this.dialogFormVisible = false;
            this.saveFlag = true;
          } else{
            this.$message.error("用户名或密码不正确！");
          }
        });
      }
       /*if(this.form1.password.length==0){
         this.$message.error("请输入清点人密码");
       }else{
            checkPwd(this.jobNo, this.form1.password).then((response) => {
            let code = response.data.code;
            if (code == "0") {
              this.$message.success("输入正确");
              this.dialogFormVisible = false;
              this.saveFlag = true;
            } else if (code == "1") {
              this.$message.error(response.data.msg);
            }
          });
       }*/
    },
  },
};
</script>
